import sys
from cmath import exp, pi
sys.path.append("/home/jmmauricio/Documents/APEIRON2012/ahkab/")


import ahkab
import circuit, printing, devices


mycircuit = circuit.circuit(title="Butterworth Example circuit", filename=None)

gnd = mycircuit.get_ground_node()

mycircuit.add_resistor(name="R1", ext_n1="n1", ext_n2=gnd, R=0.001)
#mycircuit.add_inductor(name="L1", ext_n1="n2", ext_n2=gnd, L=1.0/(2.*3.1415926*50.))


voltage_step = devices.sin(vo=0, va=440., freq=50.0, td=0, theta=0.0)
mycircuit.add_vsource(name="V1", ext_n1="n1", ext_n2=gnd, vdc=0., vac=100., function=voltage_step)

printing.print_circuit(mycircuit)

    
op_analysis = {"type":"op", "guess_label":None}
ac_analysis = {"type":"ac", "start":50, "stop":50, "nsteps":1}
#tran_analysis = {"type":"tran", "tstart":0, "tstop":0.2, "tstep":100e-6, "uic":0, "method":None, "ic_label":None}
r = ahkab.process_analysis(an_list=[op_analysis, ac_analysis], circ=mycircuit, outfile="script_output", verbose=0, cli_tran_method=None, guess=True, disable_step_control=False)

#print (r['ac']['|Vn2|']*exp()-r['ac']['|Vn1|'])/mycircuit.elements[0].R



##import pylab
##
##fig = pylab.figure()
##pylab.title(mycircuit.title + " - TRAN Simulation")
##pylab.plot(r['tran']['T'].T, r['tran']['VN1'].T, label="Input voltage")
##pylab.hold(True)
##pylab.plot(r['tran']['T'].T, r['tran']['VN2'].T, label="output voltage")
##pylab.legend()
##pylab.hold(False)
##pylab.grid(True)
###pylab.ylim([0,1.2])
##pylab.ylabel('Step response')
##pylab.xlabel('Time [s]')
##fig.savefig('tran_plot.png')
####
##fig = pylab.figure()
##pylab.subplot(211)
##pylab.semilogx(r['ac']['w'].T, r['ac']['|Vn2|'].T, 'o-')
##pylab.ylabel('abs(V(n2)) [V]')
##pylab.title(mycircuit.title + " - AC Simulation")
##pylab.subplot(212)
##pylab.grid(True)
##pylab.semilogx(r['ac']['w'].T, r['ac']['arg(Vn2)'].T, 'o-')
##pylab.xlabel('Angular frequency [rad/s]')
##pylab.ylabel('arg(V(n2)) [rad]')
##fig.savefig('ac_plot.png')
##pylab.show()
